package com.aurora.oa.message.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.message.bean.MessageTypeBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * @Class: ListAdapter
 * @Description: 数据适配器
 */
public class ImageSelectAdapter extends RecyclerView.Adapter<ImageSelectAdapter.ItemViewHolder> {

    private List<Uri> imageUris;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public ImageSelectAdapter(Context context, List<Uri> imageUris) {
        this.imageUris = imageUris;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemCount() {
        return imageUris.size() +1;
    }

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(final ItemViewHolder itemViewHolder, final int i) {
        if(i!=imageUris.size()){
            itemViewHolder.sdv_upload_img.setImageURI(imageUris.get(i));
        }

        if(mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if(!itemViewHolder.itemView.hasOnClickListeners()) {
                Log.e("ListAdapter", "setOnClickListener");
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = itemViewHolder.getPosition();
                        mOnItemClickListener.onItemClick(v, pos);
                    }
                });
                itemViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = itemViewHolder.getPosition();
                        mOnItemClickListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        /**
         * 使用RecyclerView，ViewHolder是可以复用的。这根使用ListView的VIewHolder复用是一样的
         * ViewHolder创建的个数好像是可见item的个数+3
         */
        ItemViewHolder holder = new ItemViewHolder(mInflater.inflate(
                R.layout.item_sdv_upload, viewGroup, false));
        return holder;
    }


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 处理item的点击事件和长按事件
     */
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
        public void onItemLongClick(View view, int position);
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {


        private SimpleDraweeView sdv_upload_img;


        public ItemViewHolder(View itemView) {
            super(itemView);
            sdv_upload_img = (SimpleDraweeView) itemView.findViewById(R.id.sdv_upload);
        }
    }

}